﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using WebApplication1.A01.Entities;
using WebApplication1.A02.EntitiesPersistent;

namespace WebApplication1.A03.EntitiesProcess
{
    public class PersonRepositor : IPersonRepository
    {
        ///<summary>
        ///提取人员数据集合的对象个数
        ///</summary>
        ///<returns></returns>
        public int Count()
        {
            return PersonStore.Persons.Count;
        }
       

        ///<summary>
        ///根据人员对象ID提取单个人员对象
        ///</summary>
        ///<paramname="id">人员对象标识</param>
        ///<returns></returns>
        public Person GetPerson(Guid id) => PersonStore.Persons.Where(x => x.ID == 
  id).FirstOrDefault();
        ///<summary>
        ///提取全部的人员对象
        ///</summary>
        ///<returns></returns>
        public List<Person> GetPersons() => PersonStore.Persons.ToList();
        ///<summary>
        ///根据关键词提取全部符合关键词检索条件的人员对象
        ///</summary>
        ///<paramname="keyword">关键词</param>
        ///<returns></returns>
        public List<Person> GetPersons(string keyword)
        {
            var persons = new List<Person>();
            if (String.IsNullOrEmpty(keyword))
            {
                persons = PersonStore.Persons.ToList();
            }
            else
            {
                persons = PersonStore.Persons.Where(x =>
                  x.Name.Contains(keyword) ||
                  x.Email.Contains(keyword) ||
                  x.Mobile.Contains(keyword) ||
                  x.Description.Contains(keyword) ||
                  x.SortCode.Contains(keyword)
                ).ToList();
            }
            return persons;
        }
        ///<summary>
        ///根据查询条件的Lambda表达式提取符合条件的人员对象集合
        ///</summary>
        ///<paramname="predicate">查询条件的Lambda表达式</param>
        ///<returns></returns>
        public List<Person> GetPersons(Expression<Func<Person, bool>> predicate)
        {
            var persons = PersonStore.Persons.AsQueryable().Where(predicate).ToList();
            return persons;
        }
        ///<summary>
        ///添加人员到数据集
        ///</summary>
        ///<paramname="person">外部创建的人员对象</param>
        public bool Add(Person person)
        {
            PersonStore.Persons.Add(person);
            return true;
        }
        ///<summary>
        ///编辑人员对象数据
        ///</summary>
        ///<paramname="person">提取自本数据集的对象加工处理后返回的对象</param>
        ///<returns></returns>
        public bool Update(Person person)
        {
            var toBeEditPerson = PersonStore.Persons.Where(x => x.ID ==
              person.ID).FirstOrDefault();
            if (toBeEditPerson == null)
                return false;
            else
            {
                toBeEditPerson.Name = person.Name;
                toBeEditPerson.Email = person.Email;
                toBeEditPerson.Mobile = person.Mobile;
                toBeEditPerson.Description = person.Description;
                toBeEditPerson.SortCode = person.SortCode;
                return true;
            }
        }
        ///<summary>
        ///根据人员对象ID删除集合中的人员对象
        ///</summary>
        ///<paramname="id"></param>
        ///<returns></returns>
        public bool Delete(Guid id)
        {
            var toBeDeletePerson = GetPerson(id);
            if (toBeDeletePerson == null)
                return false;
            else
            {
                PersonStore.Persons.Remove(toBeDeletePerson);
                return true;
            }
        }
    }
}
